package com.woniu.exception;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@ControllerAdvice("com.woniuxy.controller")
public class GlobalException {
    @ExceptionHandler(Throwable.class)
    public void HandLerException(Throwable e, HttpServletResponse response){
        e.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",500);
        map.put("msg","系统异常，请联系管理员");
        ObjectMapper objectMapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        try {

            String s = objectMapper.writeValueAsString(map);
            response.getWriter().write(s);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

    }

}
